package com.csii.core.system.aspect;

import com.csii.core.annotation.PermissionData;
import com.csii.core.api.Result;
import com.csii.core.system.util.DataAutorUtils;
import com.csii.core.system.vo.SysPermissionDataRuleModel;
import com.csii.core.util.SpringContextUtils;
import com.csii.core.util.oConvertUtils;
import com.csii.user.service.ISysUserBaseService;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Method;
import java.util.List;

/**
  * 数据权限切面处理类
  *  当被请求的方法有注解PermissionData时,会在往当前request中写入数据权限信息
 * @Date 2019年4月10日
 * @Version: 1.0
 */
@Aspect
@Component
@Slf4j
public class PermissionDataAspect {

	@Autowired(required = false)
	private ISysUserBaseService sysUserBaseService;
	
	@Pointcut("@annotation(com.csii.core.annotation.PermissionData)")
	public void pointCut() {
		
	}
	
	@Around("pointCut()")
	public Object arround(ProceedingJoinPoint point) throws  Throwable{
		HttpServletRequest request = SpringContextUtils.getHttpServletRequest();
		MethodSignature signature = (MethodSignature) point.getSignature();
		Method method = signature.getMethod();
		PermissionData pd = method.getAnnotation(PermissionData.class);
		String component = pd.pageComponent();
		String requestPath = request.getRequestURI().substring(request.getContextPath().length());
		Result<List<SysPermissionDataRuleModel>> dataRules= listDataRuleByUrl(filterUrl(requestPath), request.getMethod(),component);
		//3.通过用户名+菜单ID 找到权限配置信息 放到request中去
		if(dataRules.isSuccess() &&dataRules.getResult()!=null && !dataRules.getResult().isEmpty()) {
			DataAutorUtils.installDataSearchConditon(request, dataRules.getResult());
			/*SysUserCacheInfo userinfo = sysUserService.getCacheUser(username);
			DataAutorUtils.installUserInfo(request, userinfo);*/
		}
		return  point.proceed();
	}

	private Result<List<SysPermissionDataRuleModel>> listDataRuleByUrl(String requestPath, String requestMethod, String component) {
		return sysUserBaseService != null ? sysUserBaseService.listDataRuleByUrl(requestPath,requestMethod,component) :sysUserBaseService.listDataRuleByUrl(filterUrl(requestPath), requestMethod,component);
	}
	
	private String filterUrl(String requestPath){
		String url = "";
		if(oConvertUtils.isNotEmpty(requestPath)){
			url = requestPath.replace("\\", "/");
			url = requestPath.replace("//", "/");
			if(url.indexOf("//")>=0){
				url = filterUrl(url);
			}
			/*if(url.startsWith("/")){
				url=url.substring(1);
			}*/
		}
		return url;
	}
	
	/**
	 * 获取请求地址
	 * @param request
	 * @return
	 */
	private String getJgAuthRequsetPath(HttpServletRequest request) {
		String queryString = request.getQueryString();
		String requestPath = request.getRequestURI();
		if(oConvertUtils.isNotEmpty(queryString)){
			requestPath += "?" + queryString;
		}
		if (requestPath.indexOf("&") > -1) {// 去掉其他参数(保留一个参数) 例如：loginController.do?login
			requestPath = requestPath.substring(0, requestPath.indexOf("&"));
		}
		if(requestPath.indexOf("=")!=-1){
			if(requestPath.indexOf(".do")!=-1){
				requestPath = requestPath.substring(0,requestPath.indexOf(".do")+3);
			}else{
				requestPath = requestPath.substring(0,requestPath.indexOf("?"));
			}
		}
		requestPath = requestPath.substring(request.getContextPath().length() + 1);// 去掉项目路径
		return filterUrl(requestPath);
	}
	
	private boolean moHuContain(List<String> list,String key){
		for(String str : list){
			if(key.contains(str)){
				return true;
			}
		}
		return false;
	}

	
}
